package com.itheima.day06;

public class Practice3 {
    /**按以下步骤要求编写代码：
     1. 定义一个“年龄异常类”：AgeException，使其继承自RuntimeException，并添加无参、String参数的构造方法；
     2. 定义一个“性别异常类”：SexException，使其继承自RuntimeException，并添加无参、String参数的构造方法；
     3. 定义一个Student类，属性：姓名、性别、年龄。
     在性别的set方法中判断是否是男/女，如果不是则抛出：性别异常
     在年龄的set方法中判断年龄是否是15--50之间，如果不是则抛出：年龄异常
     4. 编写测试类，创建一个Student对象，并在try{}中调用setXxx()方法为对象属性赋值，在catch()中打印年龄错误。*/
    public static void main(String[] args) {
        try {
            Student s1 =new Student();
            s1.setAge(100);
            s1.setSex('n');
        } catch ( AgeException a) {
            System.out.println("年龄异常");
            a.printStackTrace();
        }catch (SexException s){
            System.out.println("性别异常");
            s.printStackTrace();
        }

    }
}
class Student{
    private int age;
    private String name;
    private char sex;

    public Student() {
    }

    public Student(int age, String name, char sex) {
       // this.age = age;
        if (age>50 ||age<15) {
            throw new AgeException("年龄异常");
        }else{this.age = age;}
        this.name = name;
        //this.sex = sex;
        if (sex=='男'||sex =='女') {
            this.sex = sex;
        }else{
            throw new SexException("年龄异常");
        }
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age>50 ||age<15) {
            throw new AgeException( );
        }else{this.age = age;}
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        if (sex=='男'||sex =='女') {
            this.sex = sex;
        }else{
            throw new SexException( );
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", sex=" + sex +
                '}';
    }
}
class SexException extends RuntimeException{
    public SexException() {
    }

    public SexException(String message) {
        super(message);
    }

    public SexException(String message, Throwable cause) {
        super(message, cause);
    }

    public SexException(Throwable cause) {
        super(cause);
    }

    public SexException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
class AgeException extends RuntimeException{
    public AgeException() {
    }

    public AgeException(String message) {
        super(message);
    }

    public AgeException(String message, Throwable cause) {
        super(message, cause);
    }

    public AgeException(Throwable cause) {
        super(cause);
    }

    public AgeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}